//
//  XGRetweetStatusViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/2/22.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import JXSegmentedView
import MJRefresh

/// 动态cell内容点击通知
let kStatusConentCellDidSelectedNotification = Notification.Name(rawValue: "kStatusConentCellDidSelectedNotification")

class XGRetweetStatusViewController: UITableViewController, JXSegmentedListContainerViewListDelegate
{
    func listView() -> UIView
    {
        return view
    }
    
    init(userId: UInt64, type: XGTopTabType)
    {
        self.userId = userId
        self.type = type
        super.init(style: .plain)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 控制器生命周期方法
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        setUpTableView()
        // 注册内部列表可用通知监听
        NotificationCenter.default.addObserver(self, selector: #selector(enableInnerScrollViewNotification), name: kEnableInnerScrollViewNotification, object: nil)
        loadData(isLoadMore: false)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
        XGPrint("我去了")
    }
    
    /// 动态视图模型数组
    private let statusList = XGStatusListViewModel()
    /// 用户id
    private let userId: UInt64
    /// 动态类型
    private let type: XGTopTabType
}

// MARK: - 数据源和代理

extension XGRetweetStatusViewController
{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        let count = statusList.numberOfRowsInSection(type: type)
        tableView.mj_footer?.isHidden = count == 0
        return count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        return statusList.tableView(tableView, cellForRowAt: indexPath, type: type)
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return statusList.tableView(tableView, heightForRowAt: indexPath, type: type)
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)
        switch type {
        case .article, .dongtai, .video, .column:
            let statusViewModel = statusList.statusViewModelForIndex(indexPath.row)
            switch statusViewModel.itemType {
            case .postContent,.commentOrQuoteContent,.commentOrQuoteOthers: // 当是用户的动态 才进行跳转
                NotificationCenter.default.post(name: kStatusConentCellDidSelectedNotification, object: statusViewModel, userInfo: nil)
            default:
                break
            }
        default:
            break
        }
    }
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        let offsetY = scrollView.contentOffset.y
        if offsetY <= 0 && tableView.isScrollEnabled {
            XGPrint("内部最小")
            // 主scrollView 禁止 子scrollView滚动
            tableView.isScrollEnabled = false
            tableView.showsVerticalScrollIndicator = false
            NotificationCenter.default.post(name: kEnableOuterScrollViewNotification, object: nil, userInfo: nil)
        }
    }
}

// MARK: - 私有扩展

private extension XGRetweetStatusViewController
{
    @objc func enableInnerScrollViewNotification() -> Void
    {
        tableView.showsVerticalScrollIndicator = true
        tableView.isScrollEnabled = true
    }
    
    /// 加载数据
    func loadData(isLoadMore: Bool) -> Void
    {
        statusList.fetchUserStatusList(type: type, userId: userId, isLoadMore: isLoadMore) { (isSuccess, hasMore) in
            hasMore ? self.tableView.mj_footer?.endRefreshing() : self.tableView.mj_footer?.endRefreshingWithNoMoreData()
             isSuccess ? self.tableView.reloadData() : ()
        }
    }
    
    func setUpTableView() -> Void
    {
        tableView.showsVerticalScrollIndicator = false
        tableView.isScrollEnabled = false
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 600.0
        statusList.registerCell(tableView: tableView, type: type)
        tableView.mj_footer = MJRefreshBackNormalFooter { [weak self] in
            self?.loadData(isLoadMore: true)
        }
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: MJRefreshFooterHeight, right: 0)
    }
}
